VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "MapInfoParser"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit
'*************************************************************************
'  This source code is provided "as is" and as an unsupported sample of
'  a Microsoft Visual Basic application that incorporates the MapPoint 2002
'  object model and ActiveX control. It can only  be used in accordance with
'  the terms and conditions of the Microsoft MapPoint 2002 and
'  Microsoft Visual Basic End User License Agreements.
'*************************************************************************

Private m_asTextLines() As String
Private m_lLineIndex As Long

Public Function GenerateShapes(ByVal sFilename As String) As Object
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Reads the given ASCII file and generates a collection of either
    '           DataPoint, Line, Rectangle, or Region objects.
    ' Assumptions:
    ' Notes:
'**********************************************************************************

    Dim bFound As Boolean
    Dim sLine As String
    Dim asWords() As String
    Dim oGeneratedShapes As Object
    Dim iNameFieldID As Integer
    
    On Error GoTo errorHandler
    
    'Populate the TextLines array, which holds all of the non-blank lines from
    'the text file.
    m_asTextLines = GetTextLines(sFilename)
    
    'Make sure we got at least 1 line.
    If UBound(m_asTextLines) >= 0 Then
        
        'Initialize the LineIndex variable.
        m_lLineIndex = 0
        
        'Look in the header information to find the "Name" field, which will
        'be used to apply a name to the generated MapPoint shape.
        iNameFieldID = GetNameFieldID
        
        'Skip forward through the rest of the header information.  Actual shape data begins
        'after the "Data" line.
        bFound = True
        
        Do Until UCase$(m_asTextLines(m_lLineIndex)) = "DATA"
            m_lLineIndex = m_lLineIndex + 1
            If m_lLineIndex = UBound(m_asTextLines) Then
                bFound = False
                Exit Do
            End If
        Loop
        
        'Make sure the "Data" line was found.  If it wasn't, then this isn't a
        'valid MapInfo file.
        If bFound = True Then
        
            'Get the next line, and break it up into separate words.
            m_lLineIndex = m_lLineIndex + 1
            sLine = m_asTextLines(m_lLineIndex)
            asWords = Split(sLine)
            
            'Check the first word in this line.  This word will determine
            'what type of shapes are in this file.  Note that we're assuming
            'that there is only 1 shape type in each file -- this assumption
            'is based on the sample files we've examined, as well as info from
            'the MapPoint people.
            Select Case UCase$(asWords(0))
                Case "REGION"
                    'This is a Region file -- get the Region data.
                    UpdateStatus "Regions Found -- Getting Shape Data"
                    Set oGeneratedShapes = GenerateRegions()
                    
                Case "POINT"
                    'This is a Point file -- get the Point data.
                    UpdateStatus "Points Found -- Getting PushPin Data"
                    Set oGeneratedShapes = GeneratePoints()
                    
                Case "PLINE"
                    'This is a Line file -- get the line data.
                    UpdateStatus "Lines Found -- Getting Shape Data"
                    Set oGeneratedShapes = GenerateLines()
                    
                Case "RECT"
                    'This is a Rectangle file -- get the data.
                    UpdateStatus "Rectangles Found -- Getting Shape Data"
                    Set oGeneratedShapes = GenerateRectangles
                
                 Case "ROUNDRECT"
                    'This is a Rounded Rectangle file -- get the data.
                    'These shapes will be rendered as a rectangle
                    MsgBox "The rounded rectangle shapes will be rendered as " _
                    & "rectangles.", vbInformation, "Rounded Rectangle To Rectangle"
                    UpdateStatus "Rectangles Found -- Getting Shape Data"
                    Set oGeneratedShapes = GenerateRectangles
                    
                Case Else
                    'This shape type isn't supported here.
                    'Returns "nothing" as shapetype therefore bSuccess is false
            End Select
        End If
    End If
    
    'Apply shape properties, such as names, which are defined in a MapInfo .MID file.
    If Not oGeneratedShapes Is Nothing Then
        UpdateStatus "Setting Shape Properties..."
        ApplyShapeProperties sFilename, oGeneratedShapes, iNameFieldID
    End If
    
    'Return the collection object.
    Set GenerateShapes = oGeneratedShapes
    
    Exit Function
errorHandler:
    MsgBox "Error in GenerateShapes" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Private Function GetNameFieldID() As Integer
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Looks through the field definitions to find one that can be used
    '           as a name for the shape.  Looks first for a field called "NAME",
    '           and if not found then it defaults to the first field.
    ' Assumptions:
    ' Notes:
'**********************************************************************************

    Dim bFound As Boolean
    Dim sLine As String
    Dim sWord As String
    Dim asWords() As String
    Dim iNumberOfFields As Integer
    Dim iFieldIndex As Integer
    Dim sFieldName As String
    Dim iNameFieldIndex As Integer
    
    On Error GoTo errorHandler
    
    bFound = True
    
    'Skip forward through the header information to find the field definitions.
    Do Until InStr(UCase$(m_asTextLines(m_lLineIndex)), "COLUMNS") <> 0
        m_lLineIndex = m_lLineIndex + 1
        If m_lLineIndex = UBound(m_asTextLines) Then
            bFound = False
            Exit Do
        End If
    Loop
    
    'Make sure we found the field definitions.
    If bFound = True Then
    
        'Get the field definitions header line.
        sLine = m_asTextLines(m_lLineIndex)
        asWords = Split(sLine)
        
        'Make sure there are 2 words.  First word should be "COLUMNS", next word should
        'define the number of fields.
        If UBound(asWords) = 1 Then
        
            'Get the number of fields.
            iNumberOfFields = Val(asWords(1))
            
            'Loop through the fields to see if one is called "NAME".
            For iFieldIndex = 1 To iNumberOfFields
            
                'Get the field line.
                sLine = m_asTextLines(m_lLineIndex + iFieldIndex)
                asWords = Split(sLine)
                
                'Make sure there is at least one word.
                If UBound(asWords) >= 0 Then
                
                    'Get the field name.
                    sFieldName = asWords(0)
                    
                    'Check to see if this field is called "NAME".
                    If UCase$(sFieldName) = "NAME" Then
                    
                        'Found one -- we'll return this index to be used to name the shapes.
                        iNameFieldIndex = iFieldIndex - 1
                        Exit For
                        
                    End If
                End If
            Next iFieldIndex
        End If
    End If
    
    'Advance the line index past the field definitions.
    m_lLineIndex = m_lLineIndex + iNumberOfFields + 1
    
    'Return the index.  Note that if we didn't find one called "NAME", then we return an index
    'of 0, which means we'll use the first field in the attribute data to name the shapes.
    GetNameFieldID = iNameFieldIndex
    
    Exit Function
errorHandler:
    MsgBox "Error in GetNameFieldID" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Private Function ApplyShapeProperties(ByVal sMIFFilename As String, _
                                      ByRef oGeneratedShapes As Object, _
                                      ByVal iNameFieldID As Integer)
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Reads the MapInfo .MID file, which contains shape properties
    '           such as shape names, and applies these properties to the appropriate
    '           objects in our object model.
    ' Assumptions:
    ' Notes: Below is a sample line from the .MID file:
    '
    '           "Washington Park",1,"CHIC"
    '
'**********************************************************************************

    Dim sFilename As String
    Dim sMIFExtension As String
    Dim sMIDFilename As String
    Dim asTextLines() As String
    Dim sLine As String
    Dim asWords() As String
    Dim sShapeName As String
    Dim lLineIndex As Long
    Dim lShapeNumber As Long
    
    On Error GoTo errorHandler
    
    'Break the filename into Filename and Extension.
    ParseFileNameAndExtension sMIFFilename, sFilename, sMIFExtension
    
    'Get the filename with the .MID extension.
    sMIDFilename = sFilename & ".MID"
    
    'Make sure the .MID file exists.  If not, then display a warning message.
    If Len(Dir(sMIDFilename)) <> 0 Then
    
        'Get an array of all the non-blank lines in the file.
        asTextLines = GetTextLines(sMIDFilename)
    
        'Make sure we got at least one line.
        If UBound(asTextLines) >= 0 Then
            
            'Loop through all the lines in the file.  Note that there is
            'one line in the .MID file for each shape in the .MIF file.
            For lLineIndex = LBound(asTextLines) To UBound(asTextLines)
                
                'Break the line into separate words.
                sLine = asTextLines(lLineIndex)
                asWords = Split(sLine, ",")
                
                'Make sure there is at least 1 word.
                If UBound(asWords) >= 0 Then
                    
                    'Get the shape name (The field names are defined in the .mif file, so we pass in
                    'the index that we found with the GetNameFieldID function).
                    sShapeName = asWords(iNameFieldID)
                    
                    'Remove any quotes from the shape name.
                    sShapeName = Replace(sShapeName, """", vbNullString)
                    
                End If
                
                'If we didn't find a shape name for some reason, then we'll invent one.
                If Len(sShapeName) = 0 Then
                    sShapeName = "Shape_" & lLineIndex + 1
                End If
                
                'Set the name of the shape.  Note that the names in the .MID file
                'are in the same order as the data points in the .MIF file.
                
                If oGeneratedShapes.Count > lLineIndex Then
                    oGeneratedShapes.Item(lLineIndex + 1).Name = sShapeName
                End If
                
            Next lLineIndex
        End If
    Else
        MsgBox "The following file was not found:" & vbCrLf & vbCrLf _
             & sMIDFilename & vbCrLf & vbCrLf _
             & "Processing will continue, but no shape names will be applied."
    End If
    
    Exit Function
errorHandler:
    MsgBox "Error in ApplyShapeProperties" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
    Resume
End Function

Private Function GenerateRegions() As SpatialDataImport.Regions
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Generates a collection of Region objects that represent the data
    '           within a MapInfo .MIF file.
    ' Assumptions:
    ' Notes: Below is a sample of the Region data from a MapInfo .MIF file:
    '
    '        Region 1
    '          5
    '        -87.616117 41.801948
    '        -87.615952 41.785603
    '        -87.606074 41.785704
    '        -87.606418 41.802075
    '        -87.616117 41.801948
    '            Pen (1,1,0)
    '            Brush (2,10079385,16777215)
    '            Center -87.611095 41.793839
    '
'**********************************************************************************

    Dim oRegion As SpatialDataImport.Region
    Dim oRegions As SpatialDataImport.Regions
    Dim sLine As String
    Dim asWords() As String
    
    On Error GoTo errorHandler
    
    'Initialize the Regions collection.
    Set oRegions = New SpatialDataImport.Regions
    
    'Loop through the file until we reach the end.
    Do Until m_lLineIndex >= UBound(m_asTextLines)
    
        'Break the line into separate words.
        sLine = m_asTextLines(m_lLineIndex)
        asWords = Split(sLine)
        
        'Make sure we're at the beginning of a Region section.
        If UCase$(asWords(0)) = "REGION" Then
        
            'Get a Region object from the data in the file.
            Set oRegion = GetRegion
            
            'If we got a valid Region object, then add it to the collection.
            If Not oRegion Is Nothing Then
                oRegions.Add oRegion
            End If
        End If
        
        'Increment the counter, then get the next Region.
        m_lLineIndex = m_lLineIndex + 1
    Loop
    
    'Return the collection object.
    Set GenerateRegions = oRegions
    
    Exit Function
errorHandler:
    MsgBox "Error in GenerateRegions" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Private Function GetRegion() As SpatialDataImport.Region
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Returns a Region object that represents the data within a .MIF file.
    ' Assumptions:
    ' Notes:
'**********************************************************************************

    Dim oRegion As SpatialDataImport.Region
    Dim oDataPoint As SpatialDataImport.DataPoint
    Dim sLine As String
    Dim asWords() As String
    Dim lNumberOfDataPoints As Long
    Dim lDataPointIndex As Long
    
    On Error GoTo errorHandler
    
    'Create a new Region object.
    Set oRegion = New SpatialDataImport.Region
    
    'Get the next line and break it into separate words.
    m_lLineIndex = m_lLineIndex + 1
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    
    'The first line after "Region" tells us how many lat/long pairs make up this
    'region.
    lNumberOfDataPoints = Val(asWords(0))
    
    'Create the DataPoint objects that will define this Region.
    For lDataPointIndex = 1 To lNumberOfDataPoints
    
        'Get the lat/long line, and break it into separate words.
        m_lLineIndex = m_lLineIndex + 1
        sLine = m_asTextLines(m_lLineIndex)
        asWords = Split(sLine)
        
        'Create a new DataPoint object.
        Set oDataPoint = New SpatialDataImport.DataPoint
        
        'First entry is the longitude, second entry is the latitude.
        oDataPoint.Longitude = Val(asWords(0))
        oDataPoint.Latitude = Val(asWords(1))
        
        'Add this DataPoint to the local collection in the Region object.
        oRegion.DataPoints.Add oDataPoint
        
    Next lDataPointIndex
    
    'Store the "extra" data for this region -- note that we're not using this
    'data for anything at this point, but it may be useful "someday".
    m_lLineIndex = m_lLineIndex + 1
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    If UCase$(asWords(0)) = "PEN" Then
        oRegion.PenData = asWords(1)
    End If
    
    m_lLineIndex = m_lLineIndex + 1
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    If UCase$(asWords(0)) = "BRUSH" Then
        oRegion.BrushData = asWords(1)
    End If
    
    m_lLineIndex = m_lLineIndex + 1
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    If UCase$(asWords(0)) = "CENTER" Then
        oRegion.CenterLongitude = asWords(1)
        oRegion.CenterLatitude = asWords(2)
    End If
        
    Set GetRegion = oRegion
    
    Exit Function
errorHandler:
    MsgBox "Error in GetRegion" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Private Function GeneratePoints() As SpatialDataImport.DataPoints
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Generates a collection of DataPoint objects that represent the data
    '           within a MapInfo .MIF file.
    ' Assumptions:
    ' Notes: Below is a sample of the Point data from a MapInfo .MIF file:
    '
    '        Point -87.655506 41.94824
    '            Symbol (44,13395711,10)
    '
'**********************************************************************************

    Dim oDataPoint As SpatialDataImport.DataPoint
    Dim oDataPoints As SpatialDataImport.DataPoints
    Dim sLine As String
    Dim asWords() As String
    
    On Error GoTo errorHandler
    
    'Initialize the DataPoints collection.
    Set oDataPoints = New SpatialDataImport.DataPoints
    
    'Loop through the file until we reach the end.
    Do Until m_lLineIndex >= UBound(m_asTextLines)
    
        'Break the line into separate words.
        sLine = m_asTextLines(m_lLineIndex)
        asWords = Split(sLine)
        
        'Make sure we're at the beginning of a Point section.
        If UCase$(asWords(0)) = "POINT" Then
        
            'Get a DataPoint object from the data in the file.
            Set oDataPoint = GetDataPoint
            
            'If we got a valid DataPoint object, then add it to the collection.
            If Not oDataPoint Is Nothing Then
                oDataPoints.Add oDataPoint
            End If
        End If
        
        'Increment the counter, then get the next Region.
        m_lLineIndex = m_lLineIndex + 1
    Loop
    
    'Return the collection object.
    Set GeneratePoints = oDataPoints
    
    Exit Function
errorHandler:
    MsgBox "Error in GeneratePoints" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Private Function GetDataPoint() As SpatialDataImport.DataPoint
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Returns a DataPoint object that represents the data within a .MIF file.
    ' Assumptions:
    ' Notes:
'**********************************************************************************

    Dim oDataPoint As SpatialDataImport.DataPoint
    Dim sLine As String
    Dim asWords() As String
    Dim lNumberOfDataPoints As Long
    Dim lDataPointIndex As Long
    
    On Error GoTo errorHandler
    
    'Create a new DataPoint object.
    Set oDataPoint = New SpatialDataImport.DataPoint
    
    'Get the next line and break it into separate words.
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    
    'Make sure this is a Point line.
    If UCase$(asWords(0)) = "POINT" Then
        
        'First entry is the longitude, second entry is the latitude.
        oDataPoint.Longitude = Val(asWords(1))
        oDataPoint.Latitude = Val(asWords(2))
        
    End If
    
    'Store the "extra" data for this point -- note that we're not using this
    'data for anything at this point, but it may be useful "someday".
    m_lLineIndex = m_lLineIndex + 1
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    If UCase$(asWords(0)) = "SYMBOL" Then
        oDataPoint.SymbolData = asWords(1)
    End If
        
    Set GetDataPoint = oDataPoint
    
    Exit Function
errorHandler:
    MsgBox "Error in GetDataPoint" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Private Function GenerateLines() As SpatialDataImport.Lines
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Generates a collection of Line objects that represent the data
    '           within a MapInfo .MIF file.
    ' Assumptions:
    ' Notes: Below is a sample of the Region data from a MapInfo .MIF file:
    '
    '        Pline 2
    '        -87.624431 41.882066
    '        -87.62778 41.881997
    '            Pen (1,2,13408665)
    '
'**********************************************************************************

    Dim oLine As SpatialDataImport.Line
    Dim oLines As SpatialDataImport.Lines
    Dim sLine As String
    Dim asWords() As String
    
    On Error GoTo errorHandler
    
    'Initialize the Lines collection.
    Set oLines = New SpatialDataImport.Lines
    
    'Loop through the file until we reach the end.
    Do Until m_lLineIndex >= UBound(m_asTextLines)
    
        'Break the line into separate words.
        sLine = m_asTextLines(m_lLineIndex)
        asWords = Split(sLine)
        
        'Make sure we're at the beginning of a Pline section.
        If UCase$(asWords(0)) = "PLINE" Then
        
            'Get a Line object from the data in the file.
            Set oLine = GetLine
            
            'If we got a valid Line object, then add it to the collection.
            If Not oLine Is Nothing Then
                oLines.Add oLine
            End If
        End If
        
        'Increment the counter, then get the next Region.
        m_lLineIndex = m_lLineIndex + 1
    Loop
    
    'Return the collection object.
    Set GenerateLines = oLines
    
    Exit Function
errorHandler:
    MsgBox "Error in GenerateLines" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Private Function GetLine() As SpatialDataImport.Line
'**********************************************************************************
    ' Date: 4/3/2001
    ' Abstract: Returns a Line object that represents the data within a .MIF file.
    ' Assumptions:
    ' Notes:
'**********************************************************************************

    Dim oLine As SpatialDataImport.Line
    Dim oDataPoint As SpatialDataImport.DataPoint
    Dim sLine As String
    Dim asWords() As String
    Dim lNumberOfDataPoints As Long
    Dim lDataPointIndex As Long
    
    On Error GoTo errorHandler
    
    'Create a new Line object.
    Set oLine = New SpatialDataImport.Line
    
    'Get the next line and break it into separate words.
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    
    'The second word in the "Pline" line tells us how many lat/long pairs make
    'up this line.
    lNumberOfDataPoints = Val(asWords(1))
    
    'Create the DataPoint objects that will define this Line.
    For lDataPointIndex = 1 To lNumberOfDataPoints
    
        'Get the lat/long line, and break it into separate words.
        m_lLineIndex = m_lLineIndex + 1
        sLine = m_asTextLines(m_lLineIndex)
        asWords = Split(sLine)
        
        'Create a new DataPoint object.
        Set oDataPoint = New SpatialDataImport.DataPoint
        
        'First entry is the longitude, second entry is the latitude.
        oDataPoint.Longitude = Val(asWords(0))
        oDataPoint.Latitude = Val(asWords(1))
        
        'Add this DataPoint to the local collection in the Line object.
        oLine.DataPoints.Add oDataPoint
    Next lDataPointIndex
    
    'Store the "extra" data for this line -- note that we're not using this
    'data for anything at this point, but it may be useful "someday".
    m_lLineIndex = m_lLineIndex + 1
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    If UCase$(asWords(0)) = "PEN" Then
        oLine.PenData = asWords(1)
    End If
    
    Set GetLine = oLine
    
    Exit Function
errorHandler:
    MsgBox "Error in GetLine" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function
Private Function GenerateRectangles() As SpatialDataImport.Rectangles
'**********************************************************************************
    ' Date: 9/25/2001
    ' Abstract: Generates a collection of Rectangle objects that represent the data
    '           within a MapInfo .MIF file.
    ' Assumptions:
    ' Notes: Below is a sample of the Rectangle data from a MapInfo .MIF file:
    '
    '   Data
    '
    '   Rect -87.580592 41.91656 -87.466214 42.063205
    '   Pen (1,2,0)
    '   Brush (2,16777215,16777215)
    '
    '
'**********************************************************************************

    Dim oRectangle As SpatialDataImport.Rectangle
    Dim oRectangles As SpatialDataImport.Rectangles
    Dim sLine As String
    Dim asWords() As String
    Dim sFirstWord As String
    
    On Error GoTo errorHandler
    
    'Initialize the Rectangles collection.
    Set oRectangles = New SpatialDataImport.Rectangles
    
    'Loop through the file until we reach the end.
    Do Until m_lLineIndex >= UBound(m_asTextLines)
    
        'Break the line into separate words.
        sLine = m_asTextLines(m_lLineIndex)
        asWords = Split(sLine)
        
        'Make sure we're at the beginning of a Rectangle or Rounded Rectangle section.
        sFirstWord = UCase$(asWords(0))
        If sFirstWord = "RECT" Or sFirstWord = "ROUNDRECT" Then
        
            'Get a Rectangle object from the data in the file.
            Set oRectangle = GetRectangle
            
            'If we got a valid Rectangle object, then add it to the collection.
            If Not oRectangle Is Nothing Then
                oRectangles.Add oRectangle
            End If
        End If
        
        'Increment the counter, then get the next Rectangle.
        m_lLineIndex = m_lLineIndex + 1
    Loop
    
    'Return the collection object.
    Set GenerateRectangles = oRectangles
    
    Exit Function
errorHandler:
    MsgBox "Error in GenerateRectangles" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

Private Function GetRectangle() As SpatialDataImport.Rectangle
'**********************************************************************************
    ' Date: 9/25/2001
    ' Abstract: Returns a Rectangle object that represents the data within a .MIF file.
    ' Assumptions:
    ' Notes:
'**********************************************************************************

    Dim oRectangle As SpatialDataImport.Rectangle
    Dim oGivenDataPoint1 As SpatialDataImport.DataPoint
    Dim oGivenDataPoint2 As SpatialDataImport.DataPoint
    Dim oImpliedDataPoint1 As SpatialDataImport.DataPoint
    Dim oImpliedDataPoint2 As SpatialDataImport.DataPoint
    Dim oEndDataPoint As SpatialDataImport.DataPoint
    Dim sLine As String
    Dim asWords() As String
    Dim lNumberOfDataPoints As Long
    Dim lDataPointIndex As Long
    
    On Error GoTo errorHandler
    
    'Create a new Rectangle object.
    Set oRectangle = New SpatialDataImport.Rectangle
    
    'Get the next line and break it into separate words.
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    
    If UBound(asWords) >= 4 Then
        
        'Create the new DataPoint objects.
        'The MIF file provides two opposing long/lat pairs from which to infer a rectangle.
        'The other two are implied by combining long/lat values.
        Set oGivenDataPoint1 = New SpatialDataImport.DataPoint
        Set oGivenDataPoint2 = New SpatialDataImport.DataPoint
        Set oImpliedDataPoint1 = New SpatialDataImport.DataPoint
        Set oImpliedDataPoint2 = New SpatialDataImport.DataPoint
        
        'This datapoint is icluded in order to complete the rectangle shape
        'Otherwise shape will show only three sides
        Set oEndDataPoint = New SpatialDataImport.DataPoint
        
        'Set the given long/lat for the first given data point.
        oGivenDataPoint1.Longitude = asWords(1)
        oGivenDataPoint1.Latitude = asWords(2)
        
        'Set the end datapoint to the same as the beginning datapoint
        oEndDataPoint.Longitude = oGivenDataPoint1.Longitude
        oEndDataPoint.Latitude = oGivenDataPoint1.Latitude
        
        'Set the given long/lat for the second given data point.
        oGivenDataPoint2.Longitude = asWords(3)
        oGivenDataPoint2.Latitude = asWords(4)
        
        'Set the long/lat for the implied datapoints using given long/lat values.
        oImpliedDataPoint1.Longitude = asWords(1)
        oImpliedDataPoint1.Latitude = asWords(4)
        oImpliedDataPoint2.Longitude = asWords(3)
        oImpliedDataPoint2.Latitude = asWords(2)
    
        'Add these DataPoints to the local collection in the Rectangle object.
        oRectangle.DataPoints.Add oGivenDataPoint1
        oRectangle.DataPoints.Add oImpliedDataPoint1
        oRectangle.DataPoints.Add oGivenDataPoint2
        oRectangle.DataPoints.Add oImpliedDataPoint2
        oRectangle.DataPoints.Add oEndDataPoint
        
    End If
    
    'Store the "extra" data for this Rectangle -- note that we're not using this
    'data for anything at this point, but it may be useful "someday".
    m_lLineIndex = m_lLineIndex + 1
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    If UCase$(asWords(0)) = "PEN" Then
        oRectangle.PenData = asWords(1)
    End If
    
    m_lLineIndex = m_lLineIndex + 1
    sLine = m_asTextLines(m_lLineIndex)
    asWords = Split(sLine)
    If UCase$(asWords(0)) = "BRUSH" Then
        oRectangle.BrushData = asWords(1)
    End If
        
    Set GetRectangle = oRectangle
    
    Exit Function
errorHandler:
    MsgBox "Error in GetRectangle" & vbCrLf _
         & "Error Number: " & Err.Number & vbCrLf _
         & "Error Description: " & Err.Description, vbExclamation, App.Title
End Function

